package com.frank.oj.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frank.model.entity.user.SystemLog;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.dto.LoginDTO;
import com.frank.oj.model.vo.UserInfoVO;
import com.frank.oj.service.admin.account.AdminAccountService;
import com.frank.oj.service.admin.system.SystemLogService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author frank
 * @Date 2024/3/27
 */
@RestController
@RequestMapping("/api/admin")
public class AdminAccountController {

    @Resource
    private AdminAccountService adminAccountService;
    @Resource
    private SystemLogService systemLogService;

    @PostMapping("/login")
    public CommonResult<UserInfoVO> login(@Validated @RequestBody LoginDTO loginDto){
        return adminAccountService.login(loginDto);
    }

    @GetMapping("/logout")
    @RequiresAuthentication
    @RequiresRoles(value = {"root","admin","problem_admin"},logical = Logical.OR)
    public CommonResult<Void> logout() {
        return adminAccountService.logout();
    }

    @GetMapping("/get-user-SystemLogs")
    @RequiresAuthentication
    @RequiresRoles(value = {"root"})
    public CommonResult<IPage<SystemLog>> getUserSystemLogs(@RequestParam(value = "currentPage",required = false) Integer currentPage,
                                                            @RequestParam(value = "limit",required = false) Integer limit,
                                                            @RequestParam(value = "userName",required = false) String userName)  {

        return CommonResult.successResponse(systemLogService.getUserSystemLogs(currentPage,limit,userName));
    }

    @DeleteMapping("forced-offline")
    @RequiresAuthentication
    @RequiresRoles(value = {"root"})
    public CommonResult<Void> forcedOffline(@RequestParam("userName") String userName) {
        adminAccountService.forcedOffline(userName);
        return CommonResult.successResponse();
    }
}
